package cn.jolyoulu.common.core.utils.other;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

/**
 * @Author: JolyouLu
 * @Date: 2023/12/17 1:17
 * @Description 日期工具类
 */
public class DateUtils {

    /**
     * 默认的日期时间
     */
    public final static String DEFAULT_PATTERN_DATE_TIME = "yyyy-MM-dd HH:mm:ss";

    /**
     * 默认的日期格式化
     */
    public final static String DEFAULT_PATTERN_DATE = "yyyy-MM-dd";

    /**
     * 字符串转换为Date
     * @param str
     * @return
     */
    public static Date str2Date(String str) throws ParseException {
        return str2Date(str,DEFAULT_PATTERN_DATE_TIME);
    }

    /**
     * 字符串转换为Date
     * @param str
     * @param pattern
     * @return
     */
    public static Date str2Date(String str,String pattern) throws ParseException {
        return new SimpleDateFormat(pattern).parse(str);
    }

    /**
     * 字符串转换为LocalDateTime
     * @param str
     * @return
     */
    public static LocalDateTime str2LocalDateTime(String str) throws ParseException {
        return str2LocalDateTime(str,DEFAULT_PATTERN_DATE_TIME);
    }

    /**
     * 字符串转换为LocalDateTime
     * @param str
     * @param pattern
     * @return
     */
    public static LocalDateTime str2LocalDateTime(String str,String pattern)  {
        return LocalDateTime.parse(str,DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 字符串转换为LocalDate
     * @param str
     * @return
     */
    public static LocalDate str2LocalDate(String str){
        return str2LocalDate(str,DEFAULT_PATTERN_DATE);
    }

    /**
     * 字符串转换为LocalDate
     * @param str
     * @param pattern
     * @return
     */
    public static LocalDate str2LocalDate(String str,String pattern)  {
        return LocalDate.parse(str,DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * LocalDateTime转换为Date
     * 使用系统时区格式化
     * @param localDateTime
     * @return
     */
    public static Date localDateTime2Date(LocalDateTime localDateTime){
        return localDateTime2Date(localDateTime,ZoneId.systemDefault());
    }

    /**
     * LocalDateTime转换为Date
     * @param localDateTime
     * @return
     */
    public static Date localDateTime2Date(LocalDateTime localDateTime,ZoneId zoneId){
        return Date.from(localDateTime.atZone(zoneId).toInstant());
    }

    /**
     * Date转换为LocalDateTime
     * 使用系统时区格式化
     * @param date
     * @return
     */
    public static LocalDateTime date2LocalDateTime(Date date){
        return date2LocalDateTime(date,ZoneId.systemDefault());
    }

    /**
     * Date转换为LocalDateTime
     * @param date
     * @return
     */
    public static LocalDateTime date2LocalDateTime(Date date,ZoneId zoneId){
        return LocalDateTime.ofInstant(date.toInstant(),zoneId);
    }

    /**
     * LocalDateTime格式化输出
     * @param localDateTime
     * @return
     */
    public static String localDateTimeFormat(LocalDateTime localDateTime){
        return localDateTimeFormat(localDateTime,DEFAULT_PATTERN_DATE_TIME);
    }

    /**
     * LocalDateTime格式化输出
     * @param localDateTime
     * @param pattern 格式化类型(如 yyyy-MM-dd HH:mm:ss)
     * @return
     */
    public static String localDateTimeFormat(LocalDateTime localDateTime,String pattern){
        return DateTimeFormatter.ofPattern(pattern).format(localDateTime);
    }

    /**
     * Date格式化输出
     * @param date
     * @return
     */
    public static String dateFormat(Date date){
        return dateFormat(date,DEFAULT_PATTERN_DATE_TIME);
    }

    /**
     * Date格式化输出
     * @param date
     * @param pattern 格式化类型(如 yyyy-MM-dd HH:mm:ss)
     * @return
     */
    public static String dateFormat(Date date,String pattern){
        return new SimpleDateFormat(pattern).format(date);
    }

    /**
     * 获取某个时间下的那个星期的第一天(星期一)
     * @param localDateTime
     * @return
     */
    public static LocalDateTime firstByWeek(LocalDateTime localDateTime){
        return localDateTime.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY))
                .withHour(0).withMinute(0).withSecond(0).withNano(0);
    }

    /**
     * 获取某个时间下的那个星期的最后一天(星期日)
     * @param localDateTime
     * @return
     */
    public static LocalDateTime lastByWeek(LocalDateTime localDateTime){
        return localDateTime.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY))
                .withHour(0).withMinute(0).withSecond(0).withNano(0);
    }
}
